package com.microcdyUser.service.impl;
import com.microcdyUser.dao.SysRoleDeptMapper;
import com.microcdy.sys.pojo.SysRoleDept;
import com.microcdyUser.service.SysRoleDeptService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import java.util.List;
/****
 * @Author:cdy
 * @Description:SysRoleDept业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class SysRoleDeptServiceImpl implements SysRoleDeptService {

    @Autowired
    private SysRoleDeptMapper sysRoleDeptMapper;


    /**
     * SysRoleDept条件+分页查询
     * @param sysRoleDept 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<SysRoleDept> findPage(SysRoleDept sysRoleDept, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(sysRoleDept);
        //执行搜索
        return new PageInfo<SysRoleDept>(sysRoleDeptMapper.selectByExample(example));
    }

    /**
     * SysRoleDept分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<SysRoleDept> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<SysRoleDept>(sysRoleDeptMapper.selectAll());
    }

    /**
     * SysRoleDept条件查询
     * @param sysRoleDept
     * @return
     */
    @Override
    public List<SysRoleDept> findList(SysRoleDept sysRoleDept){
        //构建查询条件
        Example example = createExample(sysRoleDept);
        //根据构建的条件查询数据
        return sysRoleDeptMapper.selectByExample(example);
    }


    /**
     * SysRoleDept构建查询对象
     * @param sysRoleDept
     * @return
     */
    public Example createExample(SysRoleDept sysRoleDept){
        Example example=new Example(SysRoleDept.class);
        Example.Criteria criteria = example.createCriteria();
        if(sysRoleDept!=null){
            // 角色ID
            if(!StringUtils.isEmpty(sysRoleDept.getRoleId())){
                    criteria.andEqualTo("roleId",sysRoleDept.getRoleId());
            }
            // 部门ID
            if(!StringUtils.isEmpty(sysRoleDept.getDeptId())){
                    criteria.andEqualTo("deptId",sysRoleDept.getDeptId());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Long id){
        sysRoleDeptMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改SysRoleDept
     * @param sysRoleDept
     */
    @Override
    public void update(SysRoleDept sysRoleDept){
        sysRoleDeptMapper.updateByPrimaryKey(sysRoleDept);
    }

    /**
     * 增加SysRoleDept
     * @param sysRoleDept
     */
    @Override
    public void add(SysRoleDept sysRoleDept){
        sysRoleDeptMapper.insert(sysRoleDept);
    }

    /**
     * 根据ID查询SysRoleDept
     * @param id
     * @return
     */
    @Override
    public SysRoleDept findById(Long id){
        return  sysRoleDeptMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询SysRoleDept全部数据
     * @return
     */
    @Override
    public List<SysRoleDept> findAll() {
        return sysRoleDeptMapper.selectAll();
    }
}
